<?php
	session_start();

	uploadPhoto();

	function uploadPhoto(){
		$extension = end(explode(".", $_FILES["uploaded-file"]["name"]));
		
		$userName = $_SESSION['userName'];

		$album = $_POST['dropdownBox'];

		$fileDir = is_dir('../users/'.$userName.'/'.$album)? '../users/'.$userName.'/'.$album : '../users/'.$userName.'/default';

		if(file_exists($fileDir.'/'.$_FILES["uploaded-file"]["name"])){
			die("File name already exists");
		}
		else{
			move_uploaded_file($_FILES["uploaded-file"]["tmp_name"], $fileDir."/".$_FILES["uploaded-file"]["name"]);
			buildJavaScriptRebuild(true);
		}
	}
	
	function buildJavaScriptRebuild($success){
		$javascript = '<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>'
			.'<script type="text/javascript" src="../js/jquery-ui-1.9.1.custom.js"></script>'
			.'<script type="text/javascript" src="../js/cameraRoll.js"></script>'
			.'<script>'
			.	'$(document).ready(function(){';
		if($success){
			$javascript .= '$("#upload-cancel").click();'
				.	'console.log("Here");';
		}
		else{
			
		}
		
		$javascript .= '});'
			.	'</script>';

		print $javascript;
	}
?>